//
//  LWStatusViewModel.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/23.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit
// 封装微博首页自定义Cell的业务逻辑处理 -> 首页Cell
class LWStatusViewModel: NSObject {
    // 微博模型对象 -> 拿到这里面的数据进行封装 -> Cell做的事情到这里来做
    var status : LWStatus?
    init(status: LWStatus) {
        super.init()
        self.status = status
        // 处理业务逻辑
        handelMBrankImage(mbrank: status.user?.mbrank ?? 0)
        retweetCountContent = handleCountConten(count: status.reposts_count, defaultTitle: "转发")
        commentCountContent = handleCountConten(count: status.comments_count, defaultTitle: "评论")
        unlikeCountContent = handleCountConten(count: status.attitudes_count, defaultTitle: "赞")
        handleRetweetContent(status: status)
        handleVerifiedTypeImage(verifiedType: status.user?.verified_type ?? 0)
        handleSourceContent(source: status.source ?? "")


    }
    // 提供会员等级图片
    var mbrankImage: UIImage?
    //转发数
    var retweetCountContent: String?
    // 评论数
    var commentCountContent: String?
    //赞
    var unlikeCountContent: String?
    //转发微博内容
    var retweetContent: String?
    //  认证类型的图片
    var verifiedTypeImage: UIImage?
    //  来源
    var sourceContent: String?

    
    var timeContent: String? {
        
        guard let createAt = status?.created_at  else {
            return nil
        }
        
        return Date.sinaDate(createAt: createAt).sinaDateString
    }
    
    
    //  处理来源数据
    private func handleSourceContent(source: String) {
        //<a href="http://app.weibo.com/t/feed/6vtZb0" rel="nofollow">微博 weibo.com</a>
        
        //  判断其字符串是否有包含特定的字符串
        if source.contains("\">") && source.contains("</") {
            
            //  代码执行到此,获取特定字符串的范围-> range
            
            let startRange = source.range(of: "\">")!
            let endRange = source.range(of: "</")!
            
            //  光标的上限索引
            let startIndex = startRange.upperBound
            //  光标的下限索引
            let endIndex = endRange.lowerBound
            
            //  获取子串
            sourceContent = "来自: " + source.substring(with: startIndex..<endIndex)
            
            
            
            
        }
    }

    //  处理认证类型等级图片逻辑
    private func handleVerifiedTypeImage(verifiedType: Int) {
        //  认证类型等级 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
        switch verifiedType {
        case 1:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
    }
    //处理会员等级逻辑
    private func  handelMBrankImage(mbrank: Int){
//        common_icon_membership_expired
        //判断参数时候在1-6 之间
        if mbrank >= 1 && mbrank <= 6{
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
    }
    // MARK: -  处理 转发 评论 👍数据显示
    private func handleCountConten(count: Int,defaultTitle: String) -> String{
        if count > 0 {
            return "\(count)"
        }else{
            return defaultTitle
        }
    }
    // 处理转发微博内容显示
    private func handleRetweetContent(status: LWStatus){
        if status.retweeted_status != nil{
            //获取转发微博的用户名
            let name = status.retweeted_status?.user?.screen_name
            //获取转发微博的内容
            let text = status.retweeted_status?.text
            if name != nil && text != nil {
                //转发内容拼接
                retweetContent = "\(name!):\(text!)"
            }
        }
    }
}





